package com.xtck.ha.fault.framework.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.List;

@Slf4j
public class DoshellUtils {
    public static int onCall(String exeFile, List<String> paramlist) {
        String osVersion= System.getProperty("os.name");;
        String exeParentPath = exeFile.substring(0,exeFile.lastIndexOf(File.separator) + 1);
        int shellExit = 404;
        try{
            //windows环境
            if(osVersion.toLowerCase().contains("windows")){
                log.info("调用windows脚本触发");
                String batFileName = "";

                /**识别python命令*/
                if (exeFile.toLowerCase().contains("python")){
                    batFileName = paramlist.get(0)+".bat";
                    exeParentPath = paramlist.get(0).substring(0,paramlist.get(0).lastIndexOf(File.separator) + 1);
                }
                else
                    batFileName = exeFile+".bat";

                File file = new File(batFileName);

                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                writer.write("@echo off\r\n");
                writer.write("@cd /d "+exeParentPath+"\r\n");
                writer.write("@%1 mshta vbscript:CreateObject(\"WScript.Shell\").Run(\"%~s0 ::\",0,FALSE)(window.close)&&exit\r\n");

                String batStr ="";
                batStr = exeFile + " ";
                if (null != paramlist && paramlist.size()>0)
                    for (String param : paramlist){
                        batStr += param + " ";
                    }

                writer.write(batStr+" "+"\r\n");
//			writer.write("start /wait "+exeFile+" "+triggerFilePath+"\r\n");
                writer.close();
                log.info("bat command:"+batStr);
                Runtime.getRuntime().exec(file.getAbsolutePath());
                return 0;
                //linux环境
            }else{
                log.info("调用Linux算法触发");
                String shFileName = "";
                /**识别python命令*/
                if (exeFile.toLowerCase().contains("python")){
                    shFileName = paramlist.get(0)+".sh";
                    exeParentPath = paramlist.get(0).substring(0,paramlist.get(0).lastIndexOf(File.separator) + 1);
                }
                else
                    shFileName = exeFile+".sh";
                File file = new File(shFileName);
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                writer.write("#!/bin/bash\n");
                writer.write("stty -echo\n");
                writer.write("source ~/.bashrc\n");
                writer.write("cd  "+exeParentPath+"\n");
                String shellStr ="";
                shellStr = exeFile + " ";
                if (null != paramlist && paramlist.size()>0)
                    for (String param : paramlist){
                        shellStr += param + " ";
                    }
                writer.write(shellStr+"\n");
                writer.close();
                log.info("shell command:"+shellStr);
                //解决脚本没有执行权限
                log.info("执行文件授权命令:chmod 777 "+file.getAbsolutePath());
                ProcessBuilder builder = new ProcessBuilder("/bin/chmod", "777",file.getAbsolutePath());
                Process process = builder.start();
                process.waitFor();
                Process shellProcess=Runtime.getRuntime().exec(file.getAbsolutePath());
                try (BufferedReader reader = new BufferedReader(new InputStreamReader(shellProcess.getInputStream()))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        System.out.println(line);
                    }
                }
                shellExit=shellProcess.waitFor();
                log.info("可执行文件执行状态："+shellExit);
                if(shellExit>0){
                    log.error(exeFile+"执行异常,异常状态："+shellExit);
                }
                return shellExit;
            }

        }catch(Exception e){
            e.printStackTrace();
            return shellExit;
        }
    }
}
